package com.itheima.service.impl;

import com.itheima.mapper.*;
import com.itheima.pojo.po.IndexVisualizationPo;
import com.itheima.service.IndexVisualizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class IndexVisualizationServiceImpl implements IndexVisualizationService {
    @Autowired
    private PrejectMapper prejectMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private ProjectReviewMapper reviewMapper;
    @Autowired
    private ProjectFundingApplicationMapper fundingApplicationMapper;
    @Autowired
    private UserprofileMapper userprofileMapper;
    /**
     * 主页台账 BaseInfo
     * @return
     */
    @Override
    public List<IndexVisualizationPo> getBaseInfo() {
        List<IndexVisualizationPo> list = new ArrayList<>();
        IndexVisualizationPo po = new IndexVisualizationPo();
        // 用户
        po = userMapper.selectBaseInfo();
        po.setTitle("新增用户");
        po.setTotalName("累计用户");
        // 计算日环比,保留两位小数，类型转换为Float
        if (po.getYesterday() == 0){
            po.setTodayRatio(0.00f);
        }else {
            po.setTodayRatio(Float.parseFloat(String.format("%.2f", (float) ((po.getToday() - po.getYesterday()) / po.getYesterday())*100)));
        }
        list.add(po);
        // 项目
        po = prejectMapper.selectBaseInfo(null);
        po.setTitle("新增项目");
        po.setTotalName("累计项目");
        // 计算日环比,保留两位小数，类型转换为Float
        if (po.getYesterday() == 0){
            po.setTodayRatio(0.00f);
        }else {
            po.setTodayRatio(Float.parseFloat(String.format("%.2f", (float) ((po.getToday() - po.getYesterday()) / po.getYesterday())*100)));
        }
        list.add(po);
        // 项目审核
        po = reviewMapper.selectBaseInfo();
        po.setTitle("项目审核");
        po.setTotalName("累计审核");
        // 计算日环比,保留两位小数，类型转换为Float
        if (po.getYesterday() == 0){
            po.setTodayRatio(0.00f);
        }else {
            po.setTodayRatio(Float.parseFloat(String.format("%.2f", (float) ((po.getToday() - po.getYesterday()) / po.getYesterday())*100)));
        }
        list.add(po);
        // 项目资金申请
        po = fundingApplicationMapper.selectBaseInfo(null);
        po.setTitle("项目资金申请");
        po.setTotalName("累计资金申请");
        // 计算日环比,保留两位小数，类型转换为Float
        if (po.getYesterday() == 0){
            po.setTodayRatio(0.00f);
        }else {
            po.setTodayRatio(Float.parseFloat(String.format("%.2f", (float) ((po.getToday() - po.getYesterday()) / po.getYesterday())*100)));
        }
        list.add(po);
        return list;
    }

    /**
     * 项目进度统计
     * @param type
     * @return
     */
    @Override
    public Object getProjectLineData(String type) {
        //thirtyday,week,month,year
        // 时间列表
        List<Object> dateList = new ArrayList<>();
        // 项目新增列表
        List<Integer> projectList = new ArrayList<>();
        // 项目累计列表
        List<Integer> totalList = new ArrayList<>();
        // 占比数据列表
        List<Integer> ratioList = new ArrayList<>();
        dateList = getDateList(type);
        // 项目数据
        List<Map<String, Object>> projectData = new ArrayList<>();
        if ("year".equals(type)){
            for (Object date : dateList) {
                // date = "2021-01" 格式
                // LocalDate startDay = LocalDate.parse(date+"-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                LocalDateTime startDay = LocalDateTime.parse(date+"-01 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                // 获取date当月的最后一天的23:59:59
                LocalDateTime endDay = startDay.with(TemporalAdjusters.lastDayOfMonth());
                endDay = endDay.withHour(23).withMinute(59).withSecond(59);
                projectData.add( prejectMapper.selectProjectMonthData(startDay,endDay));
            }
        }else {
            for (Object date : dateList) {
                // date = LocalDate 格式 要转换为 LocalDateTime 格式
                // 获取date当天最晚的23:59:59
                LocalDateTime endDay= LocalDateTime.parse(date.toString() + " 23:59:59", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                LocalDateTime startDay= LocalDateTime.parse(date.toString() + " 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                projectData.add( prejectMapper.selectProjectThirtydayData(startDay,endDay));
            }
        }
        projectData.forEach(item->{
            Integer create = Integer.valueOf(item.get("creates").toString());
            projectList.add(create);
            Integer total = Integer.valueOf(item.get("total").toString());
            totalList.add(total);
            if (total==0){
                ratioList.add(0);
            }else {
                ratioList.add((int) ((float) (create* 100/total )));
            }
        });
        IndexVisualizationPo po = new IndexVisualizationPo();
        po.setProjectList(projectList);
        po.setTotalList(totalList);
        po.setRatioList(ratioList);
        po.setDateList(dateList);
        return po;
    }

    // 获取时间列表
    private List<Object> getDateList(String type){
        List<Object> dateList = new ArrayList<>();
        LocalDate date = LocalDate.now();
        if ("thirtyday".equals(type)){
            // 30天时间列表 
            for (int i = 30; i > 0; i--) {
                dateList.add(date.minusDays(i));
            }
        }else if ("week".equals(type)){
            // 7天时间列表
            for (int i = 7; i > 0; i--) {
                dateList.add(date.minusDays(i));
            }
        }else if ("month".equals(type)){
            // 获取本月的时间列表,从1号开始 到今天的前一天 格式 yyyy-MM-dd 2021-01-01,2021-01-02
            LocalDate startDay = date.with(TemporalAdjusters.firstDayOfMonth());
            while (startDay.isBefore(date)) {
                dateList.add(startDay);
                startDay = startDay.plusDays(1);
            }
        }else if ("year".equals(type)){
            //获取今年的每一个月的时间列表 格式 yyyy-MM，从1月开始到当前月
            int year = date.getYear();
            int month = date.getMonthValue();
            for (int i = 1; i <= month; i++) {
                dateList.add(String.valueOf(year+"-"+String.format("%02d", i)));
            }
        }
        return dateList;
        }

    /**
     * 获取用户台账信息
      * @param userId
     * @return
     */
    @Override
    public List<IndexVisualizationPo> getBaseUserInfo(Integer userId) {
        // 访问量 创建项目 项目提交 资金申请
        List<IndexVisualizationPo> list = new ArrayList<>();
        IndexVisualizationPo po = new IndexVisualizationPo();
        // 根据userId查询用户项目id列表
        List<Integer> projectIds = prejectMapper.selectProjectIdsByUserId(userId);
        // 访问量
        if (projectIds.size() == 0) return list;
        po = userprofileMapper.selectBaseInfo(projectIds);
        if (po==null){
            po = new IndexVisualizationPo();
            po.setToday(0);
            po.setYesterday(0);
            po.setTotal(0);
        }
        po.setTitle("新增访问");
        po.setTotalName("累计访问");
        // 计算日环比,保留两位小数，类型转换为Float
        if (po.getYesterday() == 0){
            po.setTodayRatio(0.00f);
        }else {
            po.setTodayRatio(Float.parseFloat(String.format("%.2f", (float) ((po.getToday() - po.getYesterday()) / po.getYesterday())*100)));
        }
        list.add(po);
        // 项目
        po = prejectMapper.selectBaseInfo(userId);
        if (po==null){
            po = new IndexVisualizationPo();
            po.setToday(0);
            po.setYesterday(0);
            po.setTotal(0);
        }
        po.setTitle("新增项目");
        po.setTotalName("累计项目");
        // 计算日环比,保留两位小数，类型转换为Float
        if (po.getYesterday() == 0){
            po.setTodayRatio(0.00f);
        }else {
            po.setTodayRatio(Float.parseFloat(String.format("%.2f", (float) ((po.getToday() - po.getYesterday()) / po.getYesterday())*100)));
        }
        list.add(po);
        // 项目提交
        po = prejectMapper.selectProjectSubmitInfo(userId);
        if (po==null){
            po = new IndexVisualizationPo();
            po.setToday(0);
            po.setYesterday(0);
            po.setTotal(0);
        }
        po.setTitle("项目提交");
        po.setTotalName("累计提交");
        // 计算日环比,保留两位小数，类型转换为Float
        if (po.getYesterday() == 0){
            po.setTodayRatio(0.00f);
        }else {
            po.setTodayRatio(Float.parseFloat(String.format("%.2f", (float) ((po.getToday() - po.getYesterday()) / po.getYesterday())*100)));
        }
        list.add(po);
        // 项目资金申请
        po = fundingApplicationMapper.selectBaseInfo(userId);
        if (po==null){
            po = new IndexVisualizationPo();
            po.setToday(0);
            po.setYesterday(0);
            po.setTotal(0);
        }
        po.setTitle("项目资金申请");
        po.setTotalName("累计资金申请");
        // 计算日环比,保留两位小数，类型转换为Float
        if (po.getYesterday() == 0){
            po.setTodayRatio(0.00f);
        }else {
            po.setTodayRatio(Float.parseFloat(String.format("%.2f", (float) ((po.getToday() - po.getYesterday()) / po.getYesterday())*100)));
        }
        list.add(po);
        return list;
    }
    /**
     * 获取教师台账信息
     * @param userId
     * @return
     */
    @Override
    public List<IndexVisualizationPo> getTeacherBaseInfo(Integer userId) {
        // 访问量 创建项目 项目提交 资金申请
        // 项目审核 资金申请 指导项目 指导项目访问
        List<IndexVisualizationPo> list = new ArrayList<>();
        IndexVisualizationPo po = new IndexVisualizationPo();
        // 根据userId查询用户项目id列表
        List<Integer> projectIds = prejectMapper.selectProjectIdsByTeacherId(userId);
        if (projectIds.size() == 0){
            return list;
        }
        // 访问量
        po = userprofileMapper.selectBaseInfo(projectIds);
        if (po==null){
            po = new IndexVisualizationPo();
            po.setToday(0);
            po.setYesterday(0);
            po.setTotal(0);
        }
        po.setTitle("新增访问");
        po.setTotalName("累计访问");
        // 计算日环比,保留两位小数，类型转换为Float
        if (po.getYesterday() == 0){
            po.setTodayRatio(0.00f);
        }else {
            po.setTodayRatio(Float.parseFloat(String.format("%.2f", (float) ((po.getToday() - po.getYesterday()) / po.getYesterday())*100)));
        }
        list.add(po);
        // 项目
        po = prejectMapper.selectBaseTeacherInfo(userId);
        if (po==null){
            po = new IndexVisualizationPo();
            po.setToday(0);
            po.setYesterday(0);
            po.setTotal(0);
        }
        po.setTitle("新增指导项目");
        po.setTotalName("累计指导项目");
        // 计算日环比,保留两位小数，类型转换为Float
        if (po.getYesterday() == 0){
            po.setTodayRatio(0.00f);
        }else {
            po.setTodayRatio(Float.parseFloat(String.format("%.2f", (float) ((po.getToday() - po.getYesterday()) / po.getYesterday())*100)));
        }
        list.add(po);
        // 项目审核
        po = reviewMapper.selectTeacherInfo(userId);
        if (po==null){
            po = new IndexVisualizationPo();
            po.setToday(0);
            po.setYesterday(0);
            po.setTotal(0);
        }
        po.setTitle("项目审核");
        po.setTotalName("累计审核");
        // 计算日环比,保留两位小数，类型转换为Float
        if (po.getYesterday() == 0){
            po.setTodayRatio(0.00f);
        }else {
            po.setTodayRatio(Float.parseFloat(String.format("%.2f", (float) ((po.getToday() - po.getYesterday()) / po.getYesterday())*100)));
        }
        list.add(po);
        // 项目资金申请
        po = fundingApplicationMapper.selectBaseTeacherInfo(userId);
        if (po==null){
            po = new IndexVisualizationPo();
            po.setToday(0);
            po.setYesterday(0);
            po.setTotal(0);
        }
        po.setTitle("新增资金审核");
        po.setTotalName("累计资金审核");
        // 计算日环比,保留两位小数，类型转换为Float
        if (po.getYesterday() == 0){
            po.setTodayRatio(0.00f);
        }else {
            po.setTodayRatio(Float.parseFloat(String.format("%.2f", (float) ((po.getToday() - po.getYesterday()) / po.getYesterday())*100)));
        }
        list.add(po);

        return list;
    }
}
